package com.eqdd.yiqidian.kotlin

import android.app.Activity
import android.support.v7.widget.RecyclerView
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpParams
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_recyclerview.*
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-6-23.
 */
abstract class BaseAddActivity : SlimBaseMultiEditActivity() {

    abstract val title: String
    abstract val url: String
    open var isNeedCompany = true


    override fun initBinding() {
        clearData()
        setContentView(R.layout.activity_recyclerview)
        initTopTitleBar(View.VISIBLE, title)

        initTopRightText("新增", View.OnClickListener {
            if (!checkParams()) {
                toast(msg ?: "")
                return@OnClickListener
            }
            val params = uploadParams()
            if (isNeedCompany) {
                params.put("comid", user.companyId)
                params.put("companyId", user.companyId)
            }
            OkGo.post<HttpResult<*>>(url)
                    .params("userGuid", user.guid)
                    .params(params)
                    .execute(object : DialogCallBack<HttpResult<*>>(this) {
                        override fun onSuccess(response: Response<HttpResult<*>>) {
                            val httpResult = response.body()
                            if (httpResult?.status == 200) {
                                toast(msg ?: "上传成功")
                                setResult(Activity.RESULT_OK)
                                finish()
                            } else {
                                toast(httpResult?.msg ?: "上传失败")
                            }
                        }
                    })
        })

    }

    override fun initRecyclerView(): RecyclerView {
        return recyclerView
    }

    abstract fun uploadParams(): HttpParams


}
